<?php
class user extends spController
{
	function index() // 这里是首页
	{
		$this->tpl_title = "管理用户和权限";
		spAddViewFunction('getavatar', 'togetavatar');
		$this->results = spClass("lib_user")->findAll();
		$this->display("admin/user_index.html");
	} 

	function write() // 撰写
	{
		$this->tpl_title = "添加新用户";
		import("spAcl.php"); // 引入Acl文件，使得可以生成加密的密码输入框
		$this->formUrl = spUrl("user", "add");
		$this->display("admin/user_write.html");
	} 

	function edit() // 修改
	{
		$this->tpl_title = "编辑用户及权限";
		import("spAcl.php"); // 引入Acl文件，使得可以生成加密的密码输入框
		$this->formUrl = spUrl("user", "update"); 
		// 这里先判断是否传入了uid
		if ($uid = $this->spArgs("uid"))
		{ 
			// 查找表，获取信息
			$this->info = spClass("lib_user")->find(array('uid' => $uid));
			$this->display("admin/user_write.html");
		} 
		else
		{ 
			// 无id则直接跳转回首页
			$this->jump(spUrl("user", "index"));
		} 
	} 

	function add() // 增加
	{
		import("spAcl.php"); // 引入Acl文件，使得可以生成加密的密码输入框
		$user = spClass("lib_user");
		$password = spClass('md5password')->pwvalue(); // 通过acl的pwvalue获取提交的加密密码
		$newrow = array(// 这里制作新增记录的值
			'name' => $this->spArgs('name'),
			'password' => $password,
			'mail' => $this->spArgs('mail'),
			'url' => $this->spArgs('url'),
			'created' => date("Y-m-d H:i:s"),
			'screenname' => $this->spArgs('screenname'),
			'phone' => $this->spArgs('phone'),
			'acl' => $this->spArgs('acl'),
			);
		$user->create($newrow);
		$this->success("新用户添加成功！", spUrl("user", "index"));
	} 

	function update() // 更新
	{
		import("spAcl.php"); // 引入Acl文件，使得可以生成加密的密码输入框
		$post = spClass("lib_user"); 
		// 这里先判断是否传入了id
		if ($uid = $this->spArgs("uid"))
		{ 
			// 执行删除
			$conditions = array("uid" => $uid);
			$password = spClass('md5password')->pwvalue(); // 通过acl的pwvalue获取提交的加密密码 
			// 如果密码为空则不更新
			if ($password == "-1" or $password == "0")
			{
				$newrow = array(
					'mail' => $this->spArgs('mail'),
					'url' => $this->spArgs('url'),
					'screenname' => $this->spArgs('screenname'),
					'phone' => $this->spArgs('phone'),
					'acl' => $this->spArgs('acl'),
					);
			} 
			else
			{
				$newrow = array(
					'mail' => $this->spArgs('mail'),
					'password' => $password,
					'url' => $this->spArgs('url'),
					'screenname' => $this->spArgs('screenname'),
					'phone' => $this->spArgs('phone'),
					'acl' => $this->spArgs('acl'),
					);
			} 

			$post->update($conditions, $newrow); // 更新记录
				$this->success("更新用户信息成功！", spUrl("admin", "index"));
		} 
	} 

	function del() // 删除用户
	{
		// 这里先判断是否传入了uid
		if ($uid = $this->spArgs("uid"))
		{ 
			// 执行删除
			spClass("lib_user")->delete(array('uid' => $uid));
			$this->success("删除成功！", spUrl("user", "index"));
		} 
		else
		{ 
			// 无id则直接跳转回首页
			$this->jump(spUrl("user", "index"));
		} 
	} 
} 
